package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.common.R;
import com.amsp.oa.oademo.entity.PersonnelArchive;
import com.amsp.oa.oademo.service.PersonnelArchiveService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "人事档案管理")
@RestController
@RequestMapping("/api/personnel-archive")
public class PersonnelArchiveController {

    @Autowired
    private PersonnelArchiveService personnelArchiveService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public R<IPage<PersonnelArchive>> getPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("姓名") @RequestParam(required = false) String employeeName,
            @ApiParam("工号") @RequestParam(required = false) String employeeNumber) {
        return R.ok(personnelArchiveService.getPage(pageNum, pageSize, employeeName, employeeNumber));
    }

    @ApiOperation("批量保存或更新")
    @PostMapping("/batch")
    public R<Void> saveOrUpdateBatch(@RequestBody List<PersonnelArchive> archives) {
        personnelArchiveService.saveOrUpdateBatchTransactional(archives);
        return R.ok();
    }

    @ApiOperation("根据ID查询")
    @GetMapping("/{id}")
    public R<PersonnelArchive> getById(@PathVariable Long id) {
        return R.ok(personnelArchiveService.getById(id));
    }

    @ApiOperation("根据ID删除")
    @DeleteMapping("/{id}")
    public R<Void> removeById(@PathVariable Long id) {
        personnelArchiveService.removeById(id);
        return R.ok();
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    @Transactional
    public R<Void> removeBatchByIds(@RequestBody List<Long> ids) {
        personnelArchiveService.removeByIds(ids);
        return R.ok();
    }
}